﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;


namespace LibraryUtility.Common
{
    /// <summary>
    /// ClassName:DES
    /// Auther:Leo.wl
    /// Time:2010-04-14
    /// DES Class
    /// </summary>
    [DebuggerStepThrough()]
    public  class DES
    {
        /// <summary>
        /// _Key is Files.
        /// </summary>
        private string _Key;
        /// <summary>
        /// Initializes a new instance of the <see cref="DES"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        public DES(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentException("des key is error!");

            _Key = ConvertKey(key);
        }
        /// <summary>
        /// Converts the key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        private string ConvertKey(string key)
        {
            if (key.Length > 8)
                return key.Substring(0, 8);
            else if (key.Length < 8)
                return key.PadRight(8, key[0]);
            else
                return key;
        }
        /// <summary>
        /// Gets the key.
        /// </summary>
        /// <value>The key.</value>
        public string Key
        {
            get
            {
                return _Key;
            }
        }
        #region
        /// <summary>
        /// 加码
        /// </summary>
        /// <param name="pToEncrypt">The p to encrypt.</param>
        /// <returns></returns>
        public string Encrypt(string pToEncrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(_Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(_Key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
                ret.AppendFormat("{0:X2}", b);
            return ret.ToString();
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="pToDecrypt">The p to decrypt.</param>
        /// <returns></returns>
        public string Decrypt(string pToDecrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(_Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(_Key);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                }
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
        }
        #endregion
    }
}
